package com.travel.modules.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.travel.commons.annotation.SysLog;
import com.travel.commons.shiro.ShiroUserGetter;
import com.travel.commons.utils.*;
import com.travel.commons.validator.ValidatorUtils;
import com.travel.commons.validator.groups.AddGroup;
import com.travel.commons.validator.groups.UpdateGroup;
import com.travel.modules.asset.entity.Goods;
import com.travel.modules.asset.entity.Hotel;
import com.travel.modules.asset.entity.ScenicArea;
import com.travel.modules.asset.service.GoodsService;
import com.travel.modules.asset.service.HotelService;
import com.travel.modules.asset.service.ScenicAreaService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/hotel")
public class SysHotelController {
    @Resource
    private HotelService hotelService;

    @Resource
    private GoodsService goodsService;

    @Resource
    private ScenicAreaService scenicAreaService;

    @RequestMapping("/list")
    public R list(@RequestBody Map<String, Object> param) {
        Integer page = (Integer) param.get("page");
        Integer limit = (Integer) param.get("limit");
        if (page == null || limit == null) {
            return R.error("缺少分页参数");
        }
        List<Hotel> list = hotelService.getAllHotel(param);
        return R.ok().put("page", PageUtils.getPage(list, limit, page));
    }

    /**
     * 根据token获取
     */
    @RequestMapping("/list/current")
    public R list2Current(@RequestBody Map<String, Object> param) {
        Integer page = (Integer) param.get("page");
        Integer limit = (Integer) param.get("limit");
        if (page == null || limit == null) {
            return R.error("缺少分页参数");
        }
        param.put("userId", ShiroUserGetter.getUserId());
        List<Hotel> list = hotelService.getAllHotel(param);
        return R.ok().put("page", PageUtils.getPage(list, limit, page));
    }

    @SysLog("酒店添加")
    @RequestMapping("/add")
    public R add(@RequestBody Hotel form) {
        form.setHotelId("h-" + new SnowNoGenerator().generate());
        form.setUserId(form.getUserId() != null ? form.getUserId() : ShiroUserGetter.getUserId());
        form.setAreaId(scenicAreaService.getOne(new QueryWrapper<ScenicArea>().eq("name", form.getAreaName()).eq("parent_area_id", 0)).getAreaId());
        form.setStatus(0);
        form.setHot(0);
        form.setReputation(0);
        form.setEconomyPrice(form.getEconomyPrice() == null ? new BigDecimal(0) : form.getEconomyPrice());
        form.setSinglePrice(form.getSinglePrice() == null ? new BigDecimal(0) : form.getSinglePrice());
        form.setDoublePrice(form.getDoublePrice() == null ? new BigDecimal(0) : form.getDoublePrice());
        form.setManyPrice(form.getManyPrice() == null ? new BigDecimal(0) : form.getManyPrice());
        form.setVipPrice(form.getVipPrice() == null ? new BigDecimal(0) : form.getVipPrice());
        ValidatorUtils.validate(form, AddGroup.class);
        hotelService.save(form);
        return R.ok().put("page", form)
                     .put("id", form.getHotelId());
    }

    @RequestMapping("/info")
    public R info(@RequestBody Hotel form) {
        String hotelId = form.getHotelId();
        if (StringUtils.isBlank(hotelId)) {
            return R.error("缺少id");
        }
        Hotel hotel = hotelService.getOne(new QueryWrapper<Hotel>().eq("hotel_id", hotelId));
        return R.ok().put("page", hotel);
    }

    @SysLog("酒店编辑")
    @RequestMapping("/edit")
    public R edit(@RequestBody Hotel form) {
        String hotelId = form.getHotelId();
        Hotel old = hotelService.getOne(new QueryWrapper<Hotel>().eq("hotel_id", hotelId));
        if (old == null) {
            return R.error("更新目标丢失");
        }
        if (StringUtils.isNotBlank(form.getAreaName())) {
            form.setAreaId(scenicAreaService.getOne(new QueryWrapper<ScenicArea>().eq("name", form.getAreaName()).eq("parent_area_id", 0)).getAreaId());
        }
        BeanUtils.copyProperties(form, old);
        ValidatorUtils.validate(old, UpdateGroup.class);
        hotelService.updateById(old);
        if (old.getStatus() != 1) {
            goodsService.remove(new QueryWrapper<Goods>().eq("goods_id", old.getHotelId()));
        } else {
            goodsService.save(Goods.builder().goodsId(form.getHotelId()).tableName(Constant.T_HOTEL).build());
        }
        log.warn("用户 {} : 酒店更新, 编号 : {}", ShiroUserGetter.getUserId(), hotelId);
        return R.ok();
    }

    /**
     * 删除指定酒店
     * @param param hotelId - 酒店id
     */
    @SysLog("酒店删除")
    @RequestMapping("/del")
    public R del(@RequestBody Map<String, String> param) {
        String hotelId = param.get("hotelId");
        if (StringUtils.isBlank(hotelId)) {
            return R.error("缺少id");
        }
        Hotel hotel = hotelService.getOne(new QueryWrapper<Hotel>().eq("hotel_id", hotelId));
        if (hotel == null) {
            return R.error("没有该记录");
        }
        goodsService.remove(new QueryWrapper<Goods>().eq("goods_id", hotelId));
        hotelService.removeById(hotel.getId());
        log.warn("用户: {}, 酒店删除, 编号: {}", ShiroUserGetter.getUserId(), hotelId);
        return R.ok();
    }
}
